package com.ruoyi.web.controller.learn;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.AnswerRecord;
import com.ruoyi.system.service.IAnswerRecordService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 答题记录表Controller
 * 
 * @author admin
 * @date 2024-03-08
 */
@Controller
@RequestMapping("/system/record")
public class AnswerRecordController extends BaseController
{
    private String prefix = "system/record";

    @Autowired
    private IAnswerRecordService answerRecordService;

    @RequiresPermissions("system:record:view")
    @GetMapping()
    public String record()
    {
        return prefix + "/record";
    }

    /**
     * 查询答题记录表列表
     */
    @RequiresPermissions("system:record:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(AnswerRecord answerRecord)
    {
        startPage();
        List<AnswerRecord> list = answerRecordService.selectAnswerRecordList(answerRecord);
        return getDataTable(list);
    }

    /**
     * 导出答题记录表列表
     */
    @RequiresPermissions("system:record:export")
    @Log(title = "答题记录表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(AnswerRecord answerRecord)
    {
        List<AnswerRecord> list = answerRecordService.selectAnswerRecordList(answerRecord);
        ExcelUtil<AnswerRecord> util = new ExcelUtil<AnswerRecord>(AnswerRecord.class);
        return util.exportExcel(list, "答题记录表数据");
    }

    /**
     * 新增答题记录表
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存答题记录表
     */
    @RequiresPermissions("system:record:add")
    @Log(title = "答题记录表", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@RequestBody List<AnswerRecord> answerRecordList)
    {
        return toAjax(answerRecordService.insertAnswerRecord(answerRecordList,getSysUser()));
    }

    /**
     * 修改答题记录表
     */
    @RequiresPermissions("system:record:edit")
    @GetMapping("/edit/{recordId}")
    public String edit(@PathVariable("recordId") Long recordId, ModelMap mmap)
    {
        AnswerRecord answerRecord = answerRecordService.selectAnswerRecordByRecordId(recordId);
        answerRecord.setUserName(getSysUser().getUserName());
        mmap.put("answerRecord", answerRecord);
        return prefix + "/edit";
    }

    /**
     * 修改保存答题记录表
     */
    @RequiresPermissions("system:record:edit")
    @Log(title = "答题记录表", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(AnswerRecord answerRecord)
    {
        return toAjax(answerRecordService.updateAnswerRecord(answerRecord));
    }

    /**
     * 删除答题记录表
     */
    @RequiresPermissions("system:record:remove")
    @Log(title = "答题记录表", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(answerRecordService.deleteAnswerRecordByRecordIds(ids));
    }
}
